package com.jinlejia.bbs.ui.gambit;

import android.content.Context;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;

import com.jinlejia.bbs.R;
import com.jinlejia.bbs.adapter.CommentsByAnswerAdapter;
import com.jinlejia.bbs.bean.CommentDetailsBean;
import com.jinlejia.bbs.databinding.BbsActivityCommentsByAnswerBinding;
import com.jinlejia.bbs.dialog.CommonDeleteDialog;
import com.jinlejia.bbs.help.CommonEvent;
import com.jinlejia.bbs.logic.CommentLogic;
import com.jinlejia.bbs.logic.CommentsByAnswerLogic;
import com.jinlejia.bbs.logic.DeleteLogic;
import com.jinlejia.bbs.widget.BBSToast;
import com.jinlejia.bbs.widget.recycler.PullInterface;

import java.util.ArrayList;

public class BBSCommentsByAnswerActivity extends AppCompatActivity implements CommonEvent.BackFilter,CommentsByAnswerLogic.LogicCallBack, PullInterface.LoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private CommentsByAnswerLogic logic;
    private BbsActivityCommentsByAnswerBinding binding;

    private String answerCode;
    private ArrayList<CommentDetailsBean> dataList;
    private DeleteLogic deleteLogic;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.bbs_activity_comments_by_answer);
        binding.setCommonEvent(CommonEvent.obtain());
        binding.setEvent(this);
        dataList = new ArrayList<>();
        deleteLogic = new DeleteLogic(this);
        logic = new CommentsByAnswerLogic(this,dataList,this);
        answerCode = getIntent().getStringExtra("answerCode");
        initView();
    }
    private CommentsByAnswerAdapter adapter;
    private void initView() {
        binding.inputLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeIme();
                binding.inputLayout.setVisibility(View.GONE);
                binding.buttonLayout.setVisibility(View.VISIBLE);
            }
        });

        adapter = new CommentsByAnswerAdapter(this);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);

        adapter.setDeleteCommentListener(new CommentsByAnswerAdapter.DeleteCommentListener() {
            @Override
            public void onDelete(String commentsCode) {
                showDeleteDialog(commentsCode);
            }
        });
        binding.recyclerView.setAdapter(adapter);
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAutoLoadMoreListener(this);
        binding.swipeRefreshLayout.setOnRefreshListener(this);
        binding.swipeRefreshLayout.post(new Runnable() {
            @Override
            public void run() {
                binding.swipeRefreshLayout.setRefreshing(true);
                logic.refresh(answerCode);
            }
        });
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    binding.sendButton.setEnabled(false);
                } else {
                    binding.sendButton.setEnabled(true);
                }
            }
        };
        binding.inputEditText.addTextChangedListener(watcher);

        binding.buttonLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                binding.buttonLayout.setVisibility(View.GONE);
                binding.inputLayout.setVisibility(View.VISIBLE);
                openIme();
            }
        });
    }

    private CommonDeleteDialog deleteDialog;
    private void showDeleteDialog(final String commentsCode){
        if (deleteDialog!=null){
            deleteDialog.dismiss();
            deleteDialog = null;
        }
        deleteDialog = new CommonDeleteDialog(this,"删除","确定删除这个评论吗？");
        deleteDialog.setConfirmListener(new CommonDeleteDialog.OnConfirmListener() {
            @Override
            public void confirm() {
                deleteLogic.deleteComment(commentsCode, new DeleteLogic.CallBack() {
                    @Override
                    public void onResult(boolean result, String code) {
                        if (result)
                            onRefresh();
                    }
                });
            }
        });
        deleteDialog.show();

    }

    private CommentLogic commentLogic;
    public void sendMessage(View view){
        Editable text = binding.inputEditText.getText();
        if (commentLogic==null){
            commentLogic = new CommentLogic(this);
        }
        CommentLogic.CommentLogicSource source = new CommentLogic.CommentLogicSource();
        source.setAnswerCode(answerCode);
        source.setCommentsContent(binding.inputEditText.getText().toString());
        commentLogic.submitMessage(source, new CommentLogic.CommentLogicCallBack() {
            @Override
            public void onResult(boolean result) {
                if (result){
                    binding.inputEditText.setText(null);
                    closeIme();
                    binding.buttonLayout.setVisibility(View.VISIBLE);
                    binding.inputLayout.setVisibility(View.GONE);
                    onRefresh();
                }
            }
        });

    }

    @Override
    public void onRefresh() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.refresh(answerCode);
    }

    @Override
    public void onResult(boolean hasNextPage) {
        adapter.initData(dataList);
        binding.swipeRefreshLayout.setRefreshing(false);
        if (!hasNextPage){
            binding.recyclerView.setAutoLoadMoreListener(null);
        }else {
            binding.recyclerView.setAutoLoadMoreListener(this);
        }
        binding.recyclerView.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void onLoadMore() {
        binding.swipeRefreshLayout.setRefreshing(true);
        logic.load(answerCode);
    }

    private void openIme() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        binding.inputEditText.requestFocus();
        if (imm != null){
            if (binding.inputLayout.getVisibility()==View.VISIBLE){
                imm.showSoftInput(binding.inputEditText, InputMethodManager.SHOW_FORCED);
            }

        }
    }

    private void closeIme() {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            if (binding.inputLayout.getVisibility()==View.VISIBLE){
                imm.hideSoftInputFromWindow(binding.inputEditText.getWindowToken(),0);
            }
        }
    }

    @Override
    protected void onDestroy() {
        closeIme();
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK){
            if (binding.inputLayout.getVisibility()==View.VISIBLE){
                binding.inputLayout.setVisibility(View.GONE);
                binding.buttonLayout.setVisibility(View.VISIBLE);
                closeIme();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public boolean onBack() {
        closeIme();
        return false;
    }
}
